package com.example.weatherapp1.data.local

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.weatherapp1.data.model.SavedWeather
import kotlinx.coroutines.flow.Flow

@Dao
interface WeatherDao {
    @Insert
    suspend fun insertWeather(weather: SavedWeather)

    @Update
    suspend fun updateWeather(weather: SavedWeather)

    @Query("DELETE FROM saved_weather WHERE id = :weatherId")
    suspend fun deleteWeather(weatherId: Int)

    @Query("DELETE FROM saved_weather WHERE cityId = :cityId")
    suspend fun deleteWeatherByCityId(cityId: Int)

    @Query("SELECT * FROM saved_weather WHERE cityId = :cityId ORDER BY updateTime DESC LIMIT 1")
    fun getLatestWeatherByCityId(cityId: Int): Flow<SavedWeather?>

    @Query("SELECT * FROM saved_weather WHERE cityId = :cityId")
    suspend fun getWeatherHistoryByCityId(cityId: Int): List<SavedWeather>
}
